package com.bbcare.nutritious.plat.service.imp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bbcare.comm.DateUtil;
import com.bbcare.comm.SeqUtil;
import com.bbcare.comm.ToolUtils;
import com.bbcare.nutritious.plat.entity.NtFoodInfo;
import com.bbcare.nutritious.plat.entity.NtFoodNutrients;
import com.bbcare.nutritious.plat.entity.NtFoodType;
import com.bbcare.nutritious.plat.service.INutritionFoodService;
import com.bbcare.nutritious.plat.store.ibatis.INutritionFoodDao;

/**
 *
 * @name:         NutritionFoodService.java
 * @author:       tao
 * @createTime:   2017年7月3日
 * @description:  食物管理服务类
 * @modify:
 *
 */
@Service
public class NutritionFoodService implements INutritionFoodService {

	public static final Logger logger = Logger.getLogger(NutritionFoodService.class);

    @Autowired
    private INutritionFoodDao iNutritionFoodDao;



	@Override
	@Transactional
	public Map<String, Object> foodModify(Map<String, Object> map)
			throws Exception {
		logger.info("in foodModify");
		Map<String,Object> rspMap = new HashMap<String,Object>();
		String id = (String)map.get("foodId");
		String foodTypeId = (String)map.get("foodTypeId");
		String remark = (String)map.get("remark");
		String foodName = (String)map.get("foodName");
		String createAuthor = (String)map.get("createAuthor");
		String foodDesc = (String)map.get("foodDesc");
		String foodUnit = (String)map.get("foodUnit");
		String foodLibraryId = (String)map.get("foodLibraryId");
		String foodPackageUnit = (String)map.get("foodPackageUnit");
		String foodContent = (String)map.get("foodContent");
		String permission = (String)map.get("permission");
		String tenantId = (String)map.get("tenantId");

		String currenTime = DateUtil.getSysDateA();
		NtFoodInfo food = new NtFoodInfo();
		if(StringUtils.isBlank(id)){//新增
			food.setId(SeqUtil.getSeqNo());
			food.setFoodTypeid(foodTypeId);
			food.setRemark(remark);
			food.setCreateAuthor(createAuthor);
			food.setCreateTime(currenTime);
			food.setFoodName(foodName);
			food.setFoodDesc(foodDesc);
			food.setFoodUnit(foodUnit);
			food.setStatus("1");
			food.setFoodLibraryid(foodLibraryId);
			food.setFoodPackageUnit(foodPackageUnit);
			food.setTenantId(tenantId);
			if(ToolUtils.isNumeric(foodContent)){
				food.setFoodContent(Float.parseFloat(foodContent));
			}
			iNutritionFoodDao.saveFood(food);

			//
			NtFoodType foodType = new NtFoodType();
			foodType.setId(SeqUtil.getSeqNo());
			foodType.setParentId(foodTypeId);
			foodType.setRemark(remark);
			foodType.setCreateAuthor(createAuthor);
			foodType.setCreateTime(currenTime);
			foodType.setTypeName(foodName);
			foodType.setStatus("1");
			if(StringUtils.isEmpty(permission)){
		        	permission = "2";
		        }
			foodType.setRefId(food.getId());
			foodType.setPermission(permission);
			foodType.setFoodLibraryid(foodLibraryId);
			foodType.setTenantId(tenantId);
			foodType.setLeafNode("1");
			iNutritionFoodDao.saveFoodType(foodType);

		}else{//修改
			food.setId(id);
			food.setRemark(remark);
			food.setFoodName(foodName);
//			food.setFoodTypeid(foodTypeId);
			food.setFoodDesc(foodDesc);
			food.setFoodUnit(foodUnit);
			food.setUpdateAuthor(createAuthor);
			food.setUpdateTime(currenTime);
			food.setFoodPackageUnit(foodPackageUnit);
			if(ToolUtils.isNumeric(foodContent)){
				food.setFoodContent(Float.parseFloat(foodContent));
			}
			iNutritionFoodDao.updateFood(food);

			//
			NtFoodType foodType = new NtFoodType();
			foodType.setParentId(foodTypeId);
			foodType.setRemark(remark);
			foodType.setCreateAuthor(createAuthor);
			foodType.setCreateTime(currenTime);
			foodType.setTypeName(foodName);
			foodType.setStatus("1");
			if(StringUtils.isEmpty(permission)){
		        	permission = "2";
		        }

			Map<String,Object> paramMap = new HashMap<String,Object>();
			paramMap.put("parentId",foodTypeId);
			paramMap.put("updateAuthor",createAuthor);
			paramMap.put("updateTime",currenTime);
			paramMap.put("refId",id);
			paramMap.put("typeName",foodName);
			paramMap.put("foodLibraryId",foodLibraryId);
			iNutritionFoodDao.updateFoodTypeByCondition(paramMap);
		}


		List<NtFoodNutrients> listSave = new ArrayList<NtFoodNutrients>();

		JSONArray list = JSONObject.parseArray(map.get("nutrients").toString());
		NtFoodNutrients foodNutrients = null;
		for(int i =0;i<list.size();i++){
			foodNutrients =  new NtFoodNutrients();
			foodNutrients.setId(SeqUtil.getSeqNo());
			foodNutrients.setCreateAuthor(createAuthor);
			foodNutrients.setCreateTime(currenTime);
			foodNutrients.setFoodId(food.getId());
			foodNutrients.setNutrientsCode(list.getJSONObject(i).getString("nutrientsCode"));
			foodNutrients.setNutrientsId(list.getJSONObject(i).getString("nutrientsId"));
			foodNutrients.setNutrientsValue(list.getJSONObject(i).getFloat("nutrientsValue"));
			if(!org.springframework.util.StringUtils.isEmpty(list.getJSONObject(i).getString("nutrientsPer"))
					&& ToolUtils.isNumeric(list.getJSONObject(i).getString("nutrientsPer").toString())){
				foodNutrients.setNutrientsPer(Integer.parseInt(list.getJSONObject(i).getString("nutrientsPer").toString()));
			}else{
				foodNutrients.setNutrientsPer(100);
			}
			listSave.add(foodNutrients);
		}

		//先删除 再增加
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("foodId",food.getId());
		iNutritionFoodDao.deleleFoodNutrient(paramMap);

		iNutritionFoodDao.saveFoodNutrientList(listSave);

		rspMap.put("foodTypeId",food.getFoodTypeid());
		rspMap.put("foodId",food.getId());
		return rspMap;
	}


}
